package com.jiaquyun.jcps.ui.main

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.view.KeyEvent
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.lifecycle.Observer
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.ViewModeActivity
import com.jiaquyun.jcps.mode.BaseViewModel
import com.lemlin.network.HttpServiceStatus
import kotlinx.android.synthetic.main.activity_config.*


class ConfigActivity : ViewModeActivity<BaseViewModel>() {

    enum class Type(val id: String) {
        //10=用户协议,11=关于我们,12=帮助反馈
        USER_AGREE(id = "10"),
        ABOUT_US(id = "11"),
        HELP_TEXT(id = "12")
    }

    companion object {
        private const val EXTRA_KEY_TITLE = "EXTRA_KEY_TITLE"
        private const val EXTRA_KEY_CONTENT = "EXTRA_KEY_CONTENT"
        private const val EXTRA_KEY_URL = "EXTRA_KEY_URL"
        private const val EXTRA_KEY_ID = "EXTRA_KEY_ID"

        fun open(activity: Activity, title: String? = null, id: String) {
            val intent: Intent = Intent(activity, ConfigActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, title)
                putExtra(EXTRA_KEY_ID, id)
            }
            activity.startActivity(intent)
        }

        fun openContent(activity: Activity, title: String, content: String) {
            val intent: Intent = Intent(activity, ConfigActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, title)
                putExtra(EXTRA_KEY_CONTENT, content)
            }
            activity.startActivity(intent)
        }

        fun openUrl(activity: Activity, title: String, url: String) {
            val intent: Intent = Intent(activity, ConfigActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, title)
                putExtra(EXTRA_KEY_URL, url)
            }
            activity.startActivity(intent)
        }
    }

    override fun viewModel(): BaseViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_config

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.colorWhite, view = flConfigBar)
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {

        ivConfigBack.setOnClickListener {
            finish()
        }

        intent?.getStringExtra(EXTRA_KEY_TITLE)?.let {
            tvConfigTitle.text = it
        }

        wvConfigWeb.webViewClient = WebViewClient()
        wvConfigWeb.settings.apply {
            //支持缩放，默认为true。
            setSupportZoom(false)
            //调整图片至适合webview的大小
            useWideViewPort = true
            // 缩放至屏幕的大小
            loadWithOverviewMode = true
            //设置默认编码
            defaultTextEncodingName = "utf-8"
            //设置自动加载图片
            loadsImagesAutomatically = true
            //允许访问文件
            allowFileAccess = true
            //开启javascript
            javaScriptEnabled = true
            //支持通过JS打开新窗口
            javaScriptCanOpenWindowsAutomatically = true
            //支持内容重新布局
            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
            //关闭webview中缓存
            cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        }


    }

    override fun initData() {
        intent?.getStringExtra(EXTRA_KEY_ID)?.let {
            viewModel.getArticle(it)
        }
        intent?.getStringExtra(EXTRA_KEY_URL)?.let {
            wvConfigWeb.loadUrl(it)
        }
    }

    override fun initViewMode() {
        viewModel.articleValueLiveData.observe(this, Observer { response ->
            if (response.statusCode == HttpServiceStatus.Status.FAILURE) {
                toast(response.statusTip)
            }
            if (response.statusCode == HttpServiceStatus.Status.SUCCESS) {
                response.httpData?.data?.let {
                    wvConfigWeb.loadData(it.content, ("text/html; charset=UTF-8"), (null))
                }
            }
        })
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && wvConfigWeb.canGoBack()) {
            wvConfigWeb.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()
        wvConfigWeb.destroy()
    }
}